/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import model.Danhmuc;
import model.DanhmucNhasanxuat;
import model.Nhasanxuat;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author HoangLiem
 */
public class DanhMucNhaSanXuatDAO {
    public static ArrayList<DanhmucNhasanxuat> getAll() {
        ArrayList<DanhmucNhasanxuat> list = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        try {
            Transaction trans = ss.getTransaction();
            String hql = "from DanhmucNhasanxuat";
            trans.begin();
            Query query = ss.createQuery(hql);
            list = (ArrayList<DanhmucNhasanxuat>)query.list();
            ss.flush();
            trans.commit();
            return list;
        } catch (Exception ex) {
            list = null;
        } finally {
            ssFac.close();
            return list;
        }
    }
    public static ArrayList<DanhmucNhasanxuat> getAllByCategoryId(Danhmuc maDanhMuc) {
        ArrayList<DanhmucNhasanxuat> list = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        try {
            Transaction trans = ss.getTransaction();
            String hql = "from DanhmucNhasanxuat dmnsx where dmnsx.danhmuc=:maDanhMuc";
            trans.begin();
            Query query = ss.createQuery(hql);
            query.setEntity("maDanhMuc", maDanhMuc);
            list = (ArrayList<DanhmucNhasanxuat>)query.list();
            ss.flush();
            trans.commit();
        } catch (Exception ex) {
            list = null;
            ex.printStackTrace();
        } finally {
            ssFac.close();
        }
        return list;
    }
    public static ArrayList<DanhmucNhasanxuat> getAllByNSXId(String maNSX) {
        ArrayList<DanhmucNhasanxuat> list = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        try {
            Transaction trans = ss.getTransaction();
            String hql = "from DanhmucNhasanxuat dmnsx where dmnsx.nhasanxuat.maNhaSanXuat=:maNSX";
            trans.begin();
            Query query = ss.createQuery(hql);
            query.setString("maNSX", maNSX);
            list = (ArrayList<DanhmucNhasanxuat>)query.list();
            ss.flush();
            trans.commit();
        } catch (Exception ex) {
            list = null;
            ex.printStackTrace();
        } finally {
            ssFac.close();
        }
        return list;
    }
    public static ArrayList<DanhmucNhasanxuat> getAllByManufacturer(Nhasanxuat nhaSanXuat) {
        ArrayList<DanhmucNhasanxuat> list = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        try {
            Transaction trans = ss.getTransaction();
            String hql = "from DanhmucNhasanxuat dmnsx where dmnsx.nhasanxuat=:nhaSanXuat";
            trans.begin();
            Query query = ss.createQuery(hql);
            query.setEntity("nhaSanXuat", nhaSanXuat);
            list = (ArrayList<DanhmucNhasanxuat>)query.list();
            ss.flush();
            trans.commit();
        } catch (Exception ex) {
            list = null;
            ex.printStackTrace();
        } finally {
            ssFac.close();
        }
        return list;
    }
    public static DanhmucNhasanxuat getACategory_Manufacturer(Danhmuc danhMuc, Nhasanxuat nhaSanXuat) {
       DanhmucNhasanxuat dmNsx = null;
       
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        try {
            Transaction trans = ss.getTransaction();
            String hql = "from DanhmucNhasanxuat dmnsx where dmnsx.danhmuc=:danhMuc and dmnsx.nhasanxuat=:nhaSanXuat";
            trans.begin();
            Query query = ss.createQuery(hql);
            query.setEntity("danhMuc", danhMuc);
            query.setEntity("nhaSanXuat", nhaSanXuat);
            dmNsx = (DanhmucNhasanxuat)query.uniqueResult();
            ss.flush();
            trans.commit();
        } catch (Exception ex) {
            dmNsx = null;
            ex.printStackTrace();
        } finally {
            ssFac.close();
        }
        return dmNsx;
    }
    public static DanhmucNhasanxuat getACategory_Manufacturer(String maDmNsx) {
       DanhmucNhasanxuat dmNsx = null;
       
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        try {
            Transaction trans = ss.getTransaction();
            String hql = "from DanhmucNhasanxuat dmnsx where dmnsx.maDanhMucNhaSanXuat=:maDmNsx";
            trans.begin();
            Query query = ss.createQuery(hql);
            query.setString("maDmNsx", maDmNsx);
            dmNsx = (DanhmucNhasanxuat)query.uniqueResult();
            ss.flush();
            trans.commit();
        } catch (Exception ex) {
            dmNsx = null;
            ex.printStackTrace();
        } finally {
            ssFac.close();
        }
        return dmNsx;
    }
    public static boolean insert(DanhmucNhasanxuat dmNsx) {
        boolean result = false;
        try {
            SessionFactory ssFac = HibernateUtil.getSessionFactory();
            Session session = ssFac.getCurrentSession();
            Transaction trans = session.beginTransaction();
            trans.begin();
            session.merge(dmNsx);
            result = true;
            trans.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            result = false;
        }
        return result;
    }

    public static String generate(Danhmuc danhMuc, Nhasanxuat nhaSanXuat) {
        String maDmNsx = "";
        if (danhMuc == null || nhaSanXuat == null) {
            return maDmNsx;
        }
        try {
            maDmNsx = danhMuc.getMaDanhMuc() + nhaSanXuat.getMaNhaSanXuat();
        } catch (Exception ex) {
            ex.printStackTrace();
            maDmNsx = "";
        }
        return maDmNsx;
    }

    public static String generate(String maDanhMuc, String maNhaSanXuat) {
        String maDmNsx = "";
        if (maDanhMuc == "" || maNhaSanXuat == "") {
            return maDmNsx;
        }
        try {
            maDmNsx = maDanhMuc + maNhaSanXuat;
        } catch (Exception ex) {
            ex.printStackTrace();
            maDmNsx = "";
        }
        return maDmNsx;
    }
}
